<template>
  <div class="commentadd">
    <div class="content">
      <div class="content-title">{{ rowdata.projectName }}</div>
      <div class="content-number">
        <div class="title">项目编号：</div>
        <div class="number">{{ rowdata.projectCode }}</div>
      </div>
    </div>
    <el-collapse v-model="activeNames">
      <!-- 基础信息 -->
      <el-collapse-item name="1">
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title">跟进详情</span>
        </template>
        <div class="detail">
          <div class="item">
            <div class="dot"></div>
            <div class="text">跟进时间</div>
            <div class="time">{{ rowdata.followTime }}</div>
          </div>
          <div class="detailcontent">
            <div class="line"></div>
            <div class="detailtext">
              <div class="title">跟进内容:</div>
              <div class="text">
                {{ rowdata.content }}
              </div>
            </div>
          </div>
          <div class="item">
            <div class="dot"></div>
            <div class="text">下次跟进时间</div>
            <div class="time">{{ rowdata.nextFollowTime }}</div>
          </div>
        </div>
      </el-collapse-item>
      <!-- 评论内容 -->
      <el-collapse-item name="2">
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title"
            >评论({{ proFollowEvaluateVoList.length }})</span
          >
        </template>
        <div
          class="commentcontent"
          v-for="(item, index) in proFollowEvaluateVoList"
          :key="index"
        >
          <div class="user">
            <div class="avater">
              <img :src="item.avatar" alt="" v-if="item.avatar" />
              <img src="@/assets/logo/logo.png" class="user-avatar" v-else />
            </div>
            <div class="name">{{ item.nickName }}</div>
          </div>
          <el-input
            v-if="item.pagetype == 'edit'"
            style="width: 100%"
            v-model="item.content"
            type="textarea"
            maxlength="50"
            placeholder="我要评论"
          ></el-input>
          <div class="comment" v-else>
            {{ item.content }}
          </div>

          <div class="commenttime">
            <div class="time">{{ item.createTime }}</div>
            <div class="operate" v-if="userStore.id == item.createUser">
              <div class="operate-item" v-if="item.pagetype == 'edit'">
                <div
                  class="text"
                  style="color: #007cef"
                  @click="editsave(index)"
                >
                  保存
                </div>
                <el-icon class="icon" color="#007cef"> <Tickets /> </el-icon>
              </div>
              <div class="operate-item" v-else>
                <div
                  class="text"
                  style="color: #ffdd84"
                  @click="editcomment(index)"
                >
                  编辑
                </div>
                <el-icon class="icon" color="#ffdd84"> <Edit /> </el-icon>
              </div>
              <div class="operate-item">
                <div
                  class="text"
                  style="color: #ff4956"
                  @click="deletecomment(index)"
                >
                  删除
                </div>
                <el-icon class="icon" color="#ff4956"> <Delete /> </el-icon>
              </div>
            </div>
          </div>
        </div>
      </el-collapse-item>
      <el-collapse-item name="3">
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title">添加评论</span>
          <div class="button">
            <el-button
              class="btn search"
              type="primary"
              style="width: 70px"
              @click="addcomment"
              >提交</el-button
            >
          </div>
        </template>
        <div class="addoperate">
          <div class="user">
            <div class="avater">
              <img :src="userStore.avatar" alt="" v-if="userStore.avatar" />
              <img src="@/assets/logo/logo.png" class="user-avatar" v-else />
            </div>
            <div class="name">{{ userStore.name }}</div>
          </div>
          <div class="textarea">
            <el-input
              style="width: 100%"
              v-model="content"
              type="textarea"
              maxlength="50"
              placeholder="我要评论"
            ></el-input>
          </div>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import {
  getProjectFollowDetail,
  addEvaluate,
  editEvaluate,
  deleteEvaluate,
} from "@/api/sales/marketing.js";
import { ElMessage, ElMessageBox } from "element-plus";
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();

let rowdata = ref({});
let proFollowEvaluateVoList = ref([]);

// 折叠栏默认展开
const activeNames = ref(["1", "2", "3", "4", "5"]);

let content = ref("");

/**
 * @description: 添加评论
 * @param {Number} id
 */
const addcomment = async () => {
  if (content.value == "") {
    ElMessage.error("请先填写评论再提交!");
    return;
  }
  let obj = {
    content: content.value,
    followId: rowdata.value.followId,
    projectId: rowdata.value.projectId,
  };

  const res = await addEvaluate(obj);
  if (res.code == 200) {
    refresh(rowdata.value);
    ElMessage.success(res.msg);
    content.value = "";
  }
};
/**
 * @description: 编辑评论
 * @param {Number} id
 */
const editcomment = async (index) => {
  proFollowEvaluateVoList.value[index].pagetype = "edit";
};
/**
 * @description: 保存评论
 * @param {Number} id
 */
const editsave = async (index) => {
  let obj = {
    followEvaluateId: proFollowEvaluateVoList.value[index].followEvaluateId,
    content: proFollowEvaluateVoList.value[index].content,
    followId: rowdata.value.followId,
    projectId: rowdata.value.projectId,
  };

  const res = await editEvaluate(obj);
  if (res.code == 200) {
    proFollowEvaluateVoList.value[index].pagetype = "detail";
    ElMessage.success(res.msg);
  }
};
/**
 * @description: 删除评论
 * @param {Number} id
 */
const deletecomment = async (index) => {
  ElMessageBox.confirm("确定删除这条评论吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    confirmButtonClass: "confirmBtnClass",
    cancelButtonClass: "cancelBtnClass",
  })
    .then(async () => {
      const res = await deleteEvaluate(
        proFollowEvaluateVoList.value[index].followEvaluateId
      );
      if (res.code === 200) {
        refresh(rowdata.value);
        ElMessage.success(res.msg);
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch(() => {});
};
/**
 * @description: 处理详情信息
 * @param {Number} id
 */
const handleDetailInfo = async (row) => {
  rowdata.value = row;
  console.log("rowdata.value ", rowdata.value);
  refresh(row);
};
/**
 * @description: 刷新数据
 */
const refresh = async (row) => {
  try {
    const res = await getProjectFollowDetail(row.followId);
    if (res.code == 200) {
      proFollowEvaluateVoList.value = res.data.proFollowEvaluateVoList;
      proFollowEvaluateVoList.value = proFollowEvaluateVoList.value.map(
        (item) => {
          item.pagetype = "detail";
          return item;
        }
      );
      console.log(
        "proFollowEvaluateVoList.value ",
        proFollowEvaluateVoList.value
      );
    }
  } catch (error) {}
};

defineExpose({
  handleDetailInfo,
});
</script>

<style scoped lang="scss">
.commentadd {
  width: 100%;
  height: 600px;
  overflow-y: scroll;

  .content {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .content-title {
      font-size: 20px;
      font-weight: 700;
      margin-right: 30px;
    }
    .content-number {
      display: flex;
      align-items: center;
    }
  }
  .detail {
    padding: 30px 0;
    .item {
      display: flex;
      align-items: center;
      .dot {
        width: 13px;
        height: 13px;
        background-color: #0179ed;
        border-radius: 50%;
        margin-right: 22px;
      }
      .text {
        margin-right: 22px;
      }
      .item {
        margin-right: 22px;
      }
    }
    .detailcontent {
      display: flex;
      margin: 15px 0;
      transform: translateX(5px);

      .line {
        width: 2px;
        background-color: #e2e2e2;
        margin-right: 30px;
        transform: scaleY(1.2);
      }
      .detailtext {
        width: calc(100% - 38px);
        border: 2px solid #f5f5f5;
        padding: 20px 30px;
      }
    }
  }
  .commentcontent {
    padding: 30px 0;
    .user {
      display: flex;
      align-items: center;
      .avater {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        overflow: hidden;
        margin-right: 20px;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
    .comment {
      margin-top: 20px;
      margin-left: 50px;
    }
    .commenttime {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 20px;
      margin-left: 50px;
      .operate {
        display: flex;
        .operate-item {
          display: flex;
          align-items: center;
          margin-left: 20px;
          .text {
            cursor: pointer;
          }
        }
      }
    }
  }
  .addoperate {
    position: relative;
    padding: 20px 0;
    .user {
      display: flex;
      align-items: center;
      .avater {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        overflow: hidden;
        margin-right: 20px;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
    .textarea {
      margin-top: 20px;
      margin-left: 50px;
    }
  }
  .button {
    transform: translate(1135px, 12px);
  }
}
</style>
